var util = require("../util");

module.exports = function(o, n) {
    var e = null, t = "", i = function(o) {
        return function() {
            if (o && e) {
                var n = e, t = o + "timmer";
                n[t] && (clearTimeout(n[t]), n[t] = undefined), n[t] = setTimeout(function() {
                    "function" == typeof n.addBehavior && n.addBehavior({
                        type: "ui.default",
                        data: {
                            level: o
                        }
                    });
                }, 100);
            }
        };
    }, a = require("../common/miniBehavior")(o) || {};
    util.ext(o.prototype, {
        initBehavior: function() {
            if (!this.hasInitBehavior && !e && void 0 !== n && n) {
                try {
                    this.getConfig("enableConsole") && "function" == typeof a.consoleBehavior && a.consoleBehavior.call(this), 
                    n && ("function" == typeof n.onKeyboardHeightChange && n.onKeyboardHeightChange(i("KeyboardHeightChange")), 
                    "function" == typeof n.onPageNotFound && n.onPageNotFound(i("PageNotFound")), "function" == typeof n.onAppShow && n.onAppShow(i("AppShow")), 
                    "function" == typeof n.onAppHide && n.onAppHide(i("AppHide"))), n && "function" == typeof n.onAppRoute && n.onAppRoute(function(o) {
                        var n = {
                            type: "navigation",
                            data: {
                                level: o && o.openType || "unknown",
                                from: t || "unknown",
                                to: o && o.path || "unknown"
                            }
                        };
                        e && "function" == typeof e.addBehavior && e.addBehavior(n), t = o && o.path || "";
                    }), n && "function" == typeof n.onError && n.onError(this.reportBehavior.bind(this));
                } catch (o) {
                    util.warn("[arms] error in initBehavior", o);
                }
                return e = this, this.hasInitBehavior = !0, this;
            }
        }
    });
};